// gallery.js
Page({
  data: {
    articleId: null,
    articleData: {
      title: '',
      images: [],
      backgroundImage: '',
      backgroundMusic: '',
      allowMute: true
    },
    isMuted: false,
    isPlaying: false,
    loading: true,
    error: false
  },

  onLoad(options) {
    const articleId = options.id
    if (articleId) {
      this.setData({ articleId })
      this.loadGalleryData(articleId)
    } else {
      this.showError('文章ID无效')
    }
  },

  // 加载图片集合数据
  loadGalleryData(articleId) {
    // 这里模拟从文章列表中获取数据
    // 实际项目中应该从API获取
    try {
      const app = getApp()
      // 从全局或本地存储获取文章数据
      this.getArticleFromPages(articleId)
    } catch (error) {
      console.error('加载数据失败:', error)
      this.showError('加载失败，请重试')
    }
  },

  // 从页面栈中获取文章数据
  getArticleFromPages(articleId) {
    const pages = getCurrentPages()
    const articlePage = pages.find(page => page.route === 'pages/article/article')
    
    if (articlePage && articlePage.data.articleList) {
      const article = articlePage.data.articleList.find(item => item.id == articleId)
      if (article && article.type === 'gallery') {
        this.setData({
          articleData: {
            title: article.title,
            images: article.images || [],
            backgroundImage: article.backgroundImage || '',
            backgroundMusic: article.backgroundMusic || '',
            allowMute: article.allowMute !== false
          },
          loading: false,
          isPlaying: !!(article.backgroundMusic), // 有背景音乐时默认播放
          isMuted: false
        })
      } else {
        this.showError('文章数据不存在或类型错误')
      }
    } else {
      this.showError('无法获取文章数据')
    }
  },

  // 显示错误
  showError(message) {
    this.setData({
      loading: false,
      error: true
    })
    // 官方文档: https://developers.weixin.qq.com/miniprogram/dev/api/ui/interaction/wx.showToast.html
    wx.showToast({
      title: message,
      icon: 'none'
    })
  },

  // 返回按钮
  goBack() {
    // 官方文档: https://developers.weixin.qq.com/miniprogram/dev/api/route/wx.navigateBack.html
    wx.navigateBack()
  },

  // 切换音乐播放
  toggleMusic() {
    // 检查是否有背景音乐
    if (!this.data.articleData.backgroundMusic) {
      // 官方文档: https://developers.weixin.qq.com/miniprogram/dev/api/ui/interaction/wx.showToast.html
      wx.showToast({
        title: '此文章没有背景音乐',
        icon: 'none',
        duration: 2000
      })
      return
    }

    if (this.data.isMuted) {
      // 当前静音，开启音乐
      this.setData({
        isMuted: false,
        isPlaying: true
      })
      // 官方文档: https://developers.weixin.qq.com/miniprogram/dev/api/ui/interaction/wx.showToast.html
      wx.showToast({
        title: '音乐已开启',
        icon: 'none',
        duration: 1000
      })
    } else {
      // 当前播放，静音
      this.setData({
        isMuted: true,
        isPlaying: false
      })
      // 官方文档: https://developers.weixin.qq.com/miniprogram/dev/api/ui/interaction/wx.showToast.html
      wx.showToast({
        title: '音乐已静音',
        icon: 'none',
        duration: 1000
      })
    }
  },

  // 图片加载完成
  onImageLoad(e) {
    console.log('图片加载完成:', e.detail)
  },

  // 图片加载错误
  onImageError(e) {
    console.error('图片加载失败:', e.detail)
    const index = e.currentTarget.dataset.index
    // 官方文档: https://developers.weixin.qq.com/miniprogram/dev/api/ui/interaction/wx.showToast.html
    wx.showToast({
      title: `第${index + 1}张图片加载失败`,
      icon: 'none'
    })
  },

  // 预览图片
  previewImage(e) {
    const index = e.currentTarget.dataset.index
    const images = this.data.articleData.images
    
    // 官方文档: https://developers.weixin.qq.com/miniprogram/dev/api/media/image/wx.previewImage.html
    wx.previewImage({
      current: images[index],
      urls: images
    })
  },

  // 音乐播放错误
  onMusicError(e) {
    console.error('音乐播放失败:', e.detail)
    // 官方文档: https://developers.weixin.qq.com/miniprogram/dev/api/ui/interaction/wx.showToast.html
    wx.showToast({
      title: '背景音乐加载失败',
      icon: 'none'
    })
  },

  // 分享功能
  onShareAppMessage() {
    return {
      title: this.data.articleData.title,
      path: `/pages/gallery/gallery?id=${this.data.articleId}`,
      imageUrl: this.data.articleData.images[0] || this.data.articleData.backgroundImage
    }
  },

  // 页面卸载时停止音乐
  onUnload() {
    // 停止背景音乐
    // 官方文档: https://developers.weixin.qq.com/miniprogram/dev/api/media/background-audio/wx.stopBackgroundAudio.html
    wx.stopBackgroundAudio()
  }
})